<?php
require_once (APPLICATION_PATH . "/../library/fpdf/fpdf.php");
require_once (APPLICATION_PATH . "/../library/fpdi/fpdi.php");
class Model_Pdffilehandler {

	public function exportFile($sorceFile){
		$pdf = new FPDI();
			
		$pageCount = $pdf->setSourceFile("paygold.pdf");
			
		unset($pdf);
		
		for($i=1;$i<$pageCount;$i++){
			$pdf2 = new FPDI();
				
			$pageCount = $pdf2->setSourceFile("paygold.pdf");
			
			$resFileName = getcwd()."/export/payg/".$i.".pdf";
			
			
			$tplIdx = $pdf2->importPage($i, '/MediaBox');
			$pdf2->addPage();
			$pdf2->useTemplate($tplIdx,0,0, 210,297);
			$pdf2->Output($resFileName,"F");
			//$pdf2->
			
			echo "DONE";			
			
			unset($pdf2);
			//unset($pdf);
		}
		


	}

	
	public function getList(){
		
		set_time_limit(0);
	
		$fl = fopen("list-correct.txt","r");
		
		$arrLine = array();
		while(($lineData = fgets($fl))!= false ){
			
			$arrLine[] = $lineData;
		}
		
		//d($arrLine);
		$stDetail = new Model_DbTable_Roster_Stafflogindetail();
		$stInfo = new Model_DbTable_Roster_Staffinfo();
		
		$arrSent = array();
		$arrNotSent = array();
		
		
		$counter = 1;
		$mail = new Model_Emailshandler();
		
	//	$lastArr = array();
	
		$lastCounter = 0;
		
		$arrNoTax = array();
		$arrNoZero = array();
		
		foreach($arrLine as $key => $v){
			$pos = strpos($v, "was withheld you may still have to lodge a tax return");
			//$fullName = "";

			if($pos !== false){
				
				$lastCounter = $counter;
				
				$fullName = trim(strtolower(substr($v,0,$pos)));
				$arrFullName = explode(" ",$fullName);
				if(count($arrFullName)>2){
				$surname = array_pop($arrFullName);
				$firstName = implode("", $arrFullName);
				$fullName =$firstName." ".$surname;
				}
				$idLine = $stDetail->getStaffByName(Model_EncryptHelper::sslPassword(trim($fullName)));
				
				if(!$idLine){$arrNotSent[] = array($counter,$fullName,"Can Not Match Name");} //{$fullName."<br />";}
				else{
					$idSt = $idLine[0]['id'];
					if($idSt){
					$emailLine = $stInfo->getStaffinfo($idSt);
					
					$email = $emailLine['email_staff'];
					
					if(strpos($email,"@") == 0 || strpos($email,".")== 0){
						
						$arrNotSent[] = array($counter,$fullName,"Email Wrong:".$email);
						//$lastArr = array($counter,$fullName,"Email Wrong:".$email);
					}
					else{
						
						$arrSent[] = array($counter,$fullName,$idSt,$email);
						//$lastArr = array($counter,$fullName,$idSt,$email); 
						
						$title = "Corrected!! Dear ".$fullName." Your 2014 Group Certificate (PAYG Summary) is Attached.";
						
						$mailBody = "Dear Colleagues<br />";
						$mailBody .= "Please ignore the PAYG Summary we sent to you on last Thursday.<br/><br/> Please see the verified PAYG summary in the attachment.<br/><br/> Sorry for all the inconvenience caused.<br/><br/>";
						$mailBody .= "Please Check The Attached PDF File For Details, If you have Issue Open The File, Please Contact Our Phone Collection Head Office on 03-95623882 For Help.<br />";
						
						
						
						
							//var_dump($arrLine[$key + 15],$key);
							
						
						
					
					
						$mail->sendAttachEmail($email, $title, $mailBody, getcwd()."/export/payg/", $counter.".pdf");
			
						
					}
					
					//echo $fullName.",".$email.",{$counter},<br />";
					
					
					}else{
						
						
						$arrNotSent[] = array($counter,$fullName,"Can Not Find ID");
						//$lastArr = array($counter,$fullName,$idSt,$email);  
						
					}
					
					
				}

			//regardless send to Not 		
				$counter++;
				
				
			}
			//
			/*
			$pos2 = strpos($v, "TOTAL TAX WITHHELD         $"); 
			
			if($pos2 !== false){
				
				echo "<br />";
				$pos3 = strpos($v,"$");
				$subStrAfter = substr($v,$pos3 + 1);
				//var_dump($subStrAfter);
				if(trim($subStrAfter)==""){
						
					//var_dump($lastArr);
					echo $lastCounter;
				$arrNoTax[] = $lastCounter;
					echo "<br />";
				}
				
				
				echo "<br />";
				$pos4 = strpos($v,"000 000 000");
				if($pos4!==false){
					
					echo $lastCounter." - ";
					$arrNoZero[] = $lastCounter;
					echo "<br />";
				}
				
				
				
			}
			
			*/
			
		}
		

		d($arrSent,$arrNotSent);
		
		/*
		foreach($arrSent as $key => $v){
			
			if(in_array($v[0],$arrNoTax)){
				
				foreach($v as $item){
					
					echo $item.",";
				}
				echo "<br />";
			}
			
			$title = "Dear Staff, The 2014 Group Certificate we sent out to you has some issue";
			$body = "Just a Quick messge let you know it and  we will resend it to you next week";
			
			$mail->sendNormalEmail($v[3], $title, $body);
			
			
		}
		
		foreach($arrNotSent as $key => $v){
				
			if(in_array($v[0],$arrNoTax)){
		
				foreach($v as $item){
						
					echo $item.",";
				}
				echo "<br />";
				

				
				
				
				
			}
				
		}		

		echo "-------------------------<br />";
		foreach($arrSent as $key => $v){
				
			if(in_array($v[0],$arrNoZero)){
		
				foreach($v as $item){
						
					echo $item.",";
				}
				echo "<br />";
				
				$title = "Dear Staff, The 2014 Group Certificate we sent out to you has some issue";
				$body = "Just a Quick messge let you know it and  we will resend it to you next week";
				
				$mail->sendNormalEmail($v[3], $title, $body);
				
			}
				
		}
		
		foreach($arrNotSent as $key => $v){
		
			if(in_array($v[0],$arrNoZero)){
		
				foreach($v as $item){
		
					echo $item.",";
				}
				echo "<br />";
			}
		
		}
		
		*/
		
	}

}

?>